
#include "commonapi.h"

void printhex(uint8_t *data, uint16_t len)
{
    uint16_t i;
    for (i = 0; i < len; i++)
    {
        printf("%02x", data[i]);
    }
    printf("\n");
}


bool parity_verify(uint8_t *data, uint16_t len)
{
    int i, j, parity_cnt = 0, byte_parity_cnt = 0;
    bool is_parity = false;
    uint8_t tmp_data;
    for (i = 0; i < len; i++)
    {
        tmp_data = data[i];
        for (j = 0; j < 8; j++)
        {
            byte_parity_cnt = byte_parity_cnt + (tmp_data & 1);
            tmp_data = tmp_data >> 1;
        }
        parity_cnt += byte_parity_cnt;
        byte_parity_cnt = 0;
    }
    return !(parity_cnt & 1);
}

esp_err_t i2c_master_init(void)
{
	int i2c_master_port = I2C_MASTER_NUM;
	i2c_config_t conf;
	conf.mode = I2C_MODE_MASTER;
	conf.sda_io_num = I2C_MASTER_SDA_IO;
	conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
	conf.scl_io_num = I2C_MASTER_SCL_IO;
	conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
	conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
	i2c_param_config(i2c_master_port, &conf);
	return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}